//
//  FXCircleLoadingView.swift
//  FXControls
//
//  Created by 范晓鑫 on 2023/4/19.
//

import UIKit

/// 圆环加载控件
class FXCircleLoadingView: UIView {
    /// 圆环的厚度，默认1.5
    var thickness: CGFloat {
        get {
            return loadingMask.lineWidth
        }
        set {
            loadingMask.lineWidth = newValue
            self.layoutLoadingMask()
        }
    }
    /// 圆环颜色，默认白色
    var color: UIColor = .white {
        didSet {
            self.loadColor()
        }
    }
    /// 动画速度，默认1.5
    var duration: TimeInterval = 1.5
    /// 是否正在动画
    private (set) var isAnimating: Bool = false
    
    private let loadingLayer = CALayer()
    private let loadingLeftLayer = CAGradientLayer()
    private let loadingRightLayer = CAGradientLayer()
    private let loadingMask = CAShapeLayer()
    private let animatingKey = "loading"
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.load()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        self.load()
    }
    
    private func load() {
        self.layer.addSublayer(loadingLayer)
        loadingLeftLayer.locations = [0.1, 1.1]
        loadingLeftLayer.startPoint = .init(x: 0.5, y: 0)
        loadingLeftLayer.endPoint = .init(x: 0.5, y: 1)
        loadingLayer.addSublayer(loadingLeftLayer)
        loadingRightLayer.locations = [0.1, 1.1]
        loadingRightLayer.startPoint = .init(x: 0.5, y: 0)
        loadingRightLayer.endPoint = .init(x: 0.5, y: 1)
        loadingLayer.addSublayer(loadingRightLayer)
        loadingMask.strokeColor = UIColor.black.cgColor
        loadingMask.fillColor = UIColor.clear.cgColor
        loadingMask.strokeStart = 0
        loadingMask.strokeEnd = 1
        loadingMask.lineWidth = 1.5
        loadingLayer.mask = loadingMask
        
        loadColor()
        layoutLoadingMask()
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        self.loadingLayer.frame = self.bounds
        let half = self.bounds.size.width / 2
        self.loadingLeftLayer.frame = .init(origin: .zero, size: .init(width: half, height: self.bounds.size.height))
        self.loadingRightLayer.frame = .init(origin: .init(x: half, y: 0), size: .init(width: half, height: self.bounds.size.height))
        self.loadingMask.frame = self.bounds
        self.layoutLoadingMask()
    }
    private func layoutLoadingMask() {
        let half = self.bounds.size.width / 2
        self.loadingMask.path = UIBezierPath(arcCenter: .init(x: half, y: half), radius: half - self.thickness / 2, startAngle: .pi, endAngle: -.pi, clockwise: false).cgPath
    }
    private func loadColor() {
        self.loadingLeftLayer.colors = [self.color.withAlphaComponent(0.55).cgColor, self.color.cgColor]
        self.loadingRightLayer.colors = [self.color.withAlphaComponent(0.55).cgColor, self.color.withAlphaComponent(0.1).cgColor]
    }
    func startAnimating() {
        if !self.isAnimating {
            self.isAnimating = true
            let loadingAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
            loadingAnimation.fromValue = 0
            loadingAnimation.toValue = 2 * CGFloat.pi
            loadingAnimation.duration = self.duration
            loadingAnimation.repeatCount = .infinity
            loadingAnimation.isRemovedOnCompletion = false
            loadingLayer.add(loadingAnimation, forKey: animatingKey)
        }
    }
    func stopAnimating() {
        if self.isAnimating {
            self.isAnimating = false
            self.loadingLayer.removeAnimation(forKey: animatingKey)
        }
    }
}
